<?php


namespace App\Http\Controllers\WebApi;


use App\Http\Controllers\WebApi\Business\ConfigBusiness;
use App\Models\ImConfig;
use App\Models\ImThirdPartyConfigs;
use App\Services\Configs\DefaultService;
use Illuminate\Http\Request;

class ConfigController
{
    /**
     * 批量设置服务端配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setServer(Request $request)
    {
        $param = $request->all();
        $param['configs'] = isset($param['configs']) ? json_decode($param['configs'], true) : [];
        if (empty($param['configs'])) {
            return errBack();
        }
        foreach ($param['configs'] as $conf) {
            if (!isset($conf['key']) || !isset($conf['value'])
                || !isset($conf['title']) || !isset($conf['desc'])) {
                return errBack();
            }
            //更新对应字段
            ImConfig::where('type', ImConfig::SERVER_TYPE)->where('key', $conf['key'])->update([
                'value' => $conf['value'],
                'title' => $conf['title'],
                'desc' => $conf['desc'],
                'updated_at' => time()
            ]);
        }
        return okBack();
    }

    /**
     * 获取服务端所有配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getServer(Request $request)
    {
        $data = ConfigBusiness::getServer();
        return okBack($data);
    }

    /**
     * 批量设置客户端配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setClient(Request $request)
    {
        $param = $request->all();
        $param['configs'] = isset($param['configs']) ? json_decode($param['configs'], true) : [];
        if (empty($param['configs'])) {
            return errBack();
        }
        foreach ($param['configs'] as $conf) {
            if (!isset($conf['key']) || !isset($conf['value'])
                || !isset($conf['title']) || !isset($conf['desc'])) {
                return errBack();
            }
            //更新对应字段
            ImConfig::where('type', ImConfig::CLIENT_TYPE)->where('key', $conf['key'])->update([
                'value' => $conf['value'],
                'title' => $conf['title'],
                'desc' => $conf['desc'],
                'updated_at' => time()
            ]);
        }
        return okBack();
    }

    /**
     * 获取所有客户端配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getClient(Request $request)
    {
        $data = ConfigBusiness::getClient();
        return okBack($data);
    }

    /**
     * 添加或编辑服务端(客户端)配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setting(Request $request)
    {
        $type = $request->get('type');
        $title = $request->get('title');
        $key = $request->get('key');
        $value = $request->get('value');
        $desc = $request->get('desc');
        if ($type <= 0) {
            return errBack('类型有误');
        }
        if (empty($title) || empty($key)) {
            return errBack('缺少参数');
        }
        $config = ImConfig::where([
            'type' => $type,
            'key' => $key
        ])->first();
        if (!$config) {
            $config = new ImConfig();
        }
        $config->type = $type;
        $config->title = $title;
        $config->key = $key;
        $config->value = $value;
        $config->desc = $desc ?? '';
        $config->save();
        return okBack();
    }

    /**
     * 添加第三方配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-07 9:46
     */
    public function addThirdParty(Request $request)
    {
        $key = $request->post('key');
        $cate = $request->post('cate');
        $value = $request->post('configs');
        $title = $request->post('title');
        $desc = $request->post('desc');
        $status = $request->post('status', 0);
        if (empty($key) || empty($value) || empty($cate) || empty($title)) {
            return errBack();
        }
        $config = new ImThirdPartyConfigs();
        $config->key = $key;
        $config->cate = $cate;
        $config->desc = $desc;
        $config->status = $status;
        $config->value = $value;
        $config->operator = $request->userInfo['id'];
        $config->title = $title;
        $config->save();
        return okBack();
    }

    /**
     * 编辑第三方配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editThirdParty(Request $request)
    {
        $key = $request->post('key');
        $cate = $request->post('cate');
        $value = $request->post('configs');
        $title = $request->post('title');
        $desc = $request->post('desc');
        $status = $request->post('status', 0);
        if (empty($key) || empty($value) || empty($cate)) {
            return errBack();
        }
        $config = ImThirdPartyConfigs::where('key', $key)->first();
        if (!$config) {
            return errBack('配置不存在');
        }
        $config->key = $key;
        $config->cate = $cate;
        $config->desc = $desc;
        $config->status = $status;
        $config->value = $value;
        $config->operator = $request->userInfo['id'];
        $config->title = $title;
        $config->save();
        return okBack();
    }

    /**
     * 获取第三方配置信息
     * 获取第三方开关
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getThirdParty(Request $request)
    {
        $key = $request->get('key');
        if (empty($key)) {
            return errBack();
        }
//        $switch = ConfigBusiness::thirdPartyFirst($key);
        $data = ImThirdPartyConfigs::with(['operator'])->where('cate', $key)
            ->orderBy('key', 'desc')->get()
            ->map(function ($val) {
                $val->open = false;
                if ($val->status == 1) {
                    $val->open = true;
                }
                return $val;
            });
        return okBack($data);
    }

    /**
     * 第三方切换开关设置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-07 10:50
     */
    public function setThirdPartySwitch(Request $request)
    {
        $type = ImConfig::THIRD_PARTY_TYPE;//第三方配置开关
        $title = $request->get('title');
        $key = $request->get('key');
        $value = $request->get('value');
        $desc = $request->get('desc');
        if (empty($title) || empty($key) || empty($value)) {
            return errBack('缺少参数');
        }
        $config = ImConfig::where([
            'type' => $type,
            'key' => $key
        ])->first();
        if (!$config) {
            $config = new ImConfig();
        }
        $config->type = $type;
        $config->title = $title;
        $config->key = $key;
        $config->value = $value;
        if (!empty($desc)) {
            $config->desc = $desc;
        }
        $config->save();
        return okBack();
    }

    /**
     * 第三方开关列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-15 17:043
     */
    public function thirdPartySwitchList(Request $request)
    {
        $data = ImConfig::where('type', ImConfig::THIRD_PARTY_TYPE)->get();
        $configs = ImThirdPartyConfigs::whereIn('cate', $data->pluck('key'))
            ->select(['key', 'cate', 'desc', 'title', 'value'])
            ->get();
        foreach ($data as $item) {
            $arr = [];
            $tmpKey = [];
            $item->templates = [];
            foreach ($configs as $val) {
                if ($val->cate == $item->key) {
                    if (!isset($tmpKey[$val->key])) {
                        //去除重复的key
                        $tmp = [
                            'value' => $val->key,
                            'name' => $val->title,
                            'desc' => $val->desc,
                            'details' => []
                        ];
                        foreach ($val->value as $k => $detail) {
                            $tmp['details'][$k] = '';
                        }
                        array_push($arr, $tmp);
                        $tmpKey[$val->key] = true;
                    }
                }
            }
            $item->templates = $arr;
        }
        return okBack($data);
    }
}
